package com.resourcestore.utils.tag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.resourcestore.action.CourseFileSrcCatePrivateAction;
import com.resourcestore.model.CourseFileSrcCatePrivate;

public class TreeSelectorCoursePrivate extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private int courseFileCateId;

	private String type;

	private String selectName;

	private String notNeedValidation;

	private int delCourseFileSrcCatePrivateId;// 在某些修改场景，与所修改项相同的项及其子项都需要从列表中删除
	
	private String target;

	public int doEndTag() throws JspException {

		JspWriter out = pageContext.getOut();

		List<CourseFileSrcCatePrivate> listMFSC = new ArrayList<CourseFileSrcCatePrivate>();
		List<CourseFileSrcCatePrivate> delList = new ArrayList<CourseFileSrcCatePrivate>();

		CourseFileSrcCatePrivateAction courseFileSrcCatePrivateAction = new CourseFileSrcCatePrivateAction();
		CourseFileSrcCatePrivate courseFileSrcCatePrivate = new CourseFileSrcCatePrivate();
		CourseFileSrcCatePrivate delMFSC= new CourseFileSrcCatePrivate();
		
		courseFileSrcCatePrivate.setCourseFileCateParentId(-1);

		try {
			StringBuffer sb = new StringBuffer();
			//sb.append(getNotNeedValidation() == "false" ? "" : "class=\"validate['required']\"");
			
			out.println("<select name=\"" + type + "." + getSelectName() + "\" id=\""
					+ getSelectName() + "\" " + sb.toString() + ">");
			if(target.equals("search")){
				out.println("<option value=\"\">全部</option>");
			}
			out.println("<option value=\"-1\">根分类（课程私有文件资源）</option>");

			listMFSC = courseFileSrcCatePrivateAction.recursiveListCourseFileSrcCatePrivateTree(courseFileSrcCatePrivate);
			
			for (CourseFileSrcCatePrivate eachMFSC : listMFSC) {// 将所有章节打印在页面上
				
				eachMFSC.setCourseFileCateName("|--"+ eachMFSC.getCourseFileCateName());
				for(int i=0;i<eachMFSC.getDepth();i++){
					eachMFSC.setCourseFileCateName("&nbsp;&nbsp;&nbsp;&nbsp;"+ eachMFSC.getCourseFileCateName());
				}

				// 得到了chapterId值，说明是修改，修改与新建的唯一不同是多一句selected="selected"
				// 只有一个selected="selected"
				String cateRemark = "";
				if(eachMFSC.getIsPreCourseCate()==1){
					cateRemark = "（预置）";
				}else if(eachMFSC.getIsPreCourseCate()==0){
					cateRemark = "（"+eachMFSC.getCourseName()+"）";
				}
				
				if (0 != courseFileCateId && courseFileCateId == eachMFSC.getCourseFileCateId()) {
					out.println("<option value=\"" + eachMFSC.getCourseFileCateId()
							+ "\" selected=\"selecetd\" >" + eachMFSC.getCourseFileCateName()+cateRemark
							+ "</option>");
				} else {
					out.println("<option value=\"" + eachMFSC.getCourseFileCateId() + "\">"
							+ eachMFSC.getCourseFileCateName() +cateRemark+ "</option>");
				}

			}
			out.println("</select>");
		} catch (IOException e) {
			e.printStackTrace();
			return super.SKIP_BODY;
		}

		return super.EVAL_PAGE;
	}

	public int getCourseFileCateId() {
		return courseFileCateId;
	}

	public void setCourseFileCateId(int courseFileCateId) {
		this.courseFileCateId = courseFileCateId;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getSelectName() {
		if (null == selectName) {
			selectName = "courseFileSrcCateId";
		}
		return selectName;
	}

	public void setSelectName(String selectName) {
		this.selectName = selectName;
	}

	public String getNotNeedValidation() {
		return notNeedValidation;
	}

	public void setNotNeedValidation(String notNeedValidation) {
		this.notNeedValidation = notNeedValidation;
	}

	public int getDelCourseFileSrcCatePrivateId() {
		return delCourseFileSrcCatePrivateId;
	}

	public void setDelCourseFileSrcCatePrivateId(int delCourseFileSrcCatePrivateId) {
		this.delCourseFileSrcCatePrivateId = delCourseFileSrcCatePrivateId;
	}

	public String getTarget() {
		return target;
	}

	public void setTarget(String target) {
		this.target = target;
	}
	
}
